package cn.iocoder.yudao.module.ems.service.maintainstandard;

import cn.iocoder.yudao.module.ems.controller.admin.supplier.vo.SupplierListReqVO;
import cn.iocoder.yudao.module.ems.convert.maintainstandard.MaintainStandardItemConvert;
import cn.iocoder.yudao.module.ems.dal.dataobject.maintainstandard.MaintainStandardItemDO;
import cn.iocoder.yudao.module.ems.dal.dataobject.supplier.SupplierDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.ems.controller.admin.maintainstandard.vo.*;
import cn.iocoder.yudao.module.ems.dal.dataobject.maintainstandard.MaintainStandardDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.ems.convert.maintainstandard.MaintainStandardConvert;
import cn.iocoder.yudao.module.ems.dal.mysql.maintainstandard.MaintainStandardMapper;
import cn.iocoder.yudao.module.ems.dal.mysql.maintainstandard.MaintainStandardItemMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ems.enums.ErrorCodeConstants.*;

/**
 * 保养标准 Service 实现类
 *
 * @author 左达成
 */
@Service
@Validated
public class MaintainStandardServiceImpl implements MaintainStandardService {

    @Resource
    private MaintainStandardMapper maintainStandardMapper;

    @Resource
    private MaintainStandardItemMapper maintainStandardItemMapper;

    @Override
    public Long createMaintainStandard(MaintainStandardCreateReqVO createReqVO) {
        // 插入
        MaintainStandardDO maintainStandard = MaintainStandardConvert.INSTANCE.convert(createReqVO);
        maintainStandardMapper.insert(maintainStandard);
        Collection<MaintainStandardItemCreateReqVO> itemList = createReqVO.getItem();
        for (MaintainStandardItemCreateReqVO item: itemList) {
            item.setStandardId(maintainStandard.getId());
            maintainStandardItemMapper.insert(MaintainStandardItemConvert.INSTANCE.convert4(item));
        }
        // 返回
        return maintainStandard.getId();
    }

    @Override
    public void updateMaintainStandard(MaintainStandardUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateMaintainStandardExists(updateReqVO.getId());
        // 更新
        MaintainStandardDO updateObj = MaintainStandardConvert.INSTANCE.convert(updateReqVO);
        Collection<MaintainStandardItemRespVO> itemList = updateReqVO.getMaintainStandardItem();
        for (MaintainStandardItemRespVO item: itemList) {
            item.setStandardId(updateReqVO.getId());
            if(item.getId() != null) {
                maintainStandardItemMapper.updateById(MaintainStandardItemConvert.INSTANCE.convert3(item));
            } else {
                maintainStandardItemMapper.insert(MaintainStandardItemConvert.INSTANCE.convert3(item));
            }
        }
        maintainStandardMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaintainStandard(Long id) {
        // 校验存在
        this.validateMaintainStandardExists(id);
        List<MaintainStandardItemDO> infoList = maintainStandardItemMapper.selectList(id);
        for(MaintainStandardItemDO info : infoList) {
            maintainStandardItemMapper.deleteById(info.getId());
        }
        // 删除
        maintainStandardMapper.deleteById(id);
    }

    private void validateMaintainStandardExists(Long id) {
        if (maintainStandardMapper.selectById(id) == null) {
            throw exception(MAINTAIN_STANDARD_NOT_EXISTS);
        }
    }

    @Override
    public MaintainStandardDO getMaintainStandard(Long id) {
        return maintainStandardMapper.selectById(id);
    }

    @Override
    public List<MaintainStandardDO> getMaintainStandardList(Collection<Long> ids) {
        return maintainStandardMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MaintainStandardDO> getMaintainStandardPage(MaintainStandardPageReqVO pageReqVO) {
        return maintainStandardMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MaintainStandardDO> getMaintainStandardList(MaintainStandardExportReqVO exportReqVO) {
        return maintainStandardMapper.selectList(exportReqVO);
    }

    @Override
    public List<MaintainStandardDO> getSimpleMaintainStandards(MaintainStandardListReqVO reqVO) {
        return maintainStandardMapper.selectList(reqVO);
    }

    @Override
    public List<MaintainStandardDO> getMaintainStandards(Collection<Long> ids) {
        return maintainStandardMapper.selectBatchIds(ids);
    }

}
